package com.ruoyi.slope.gateway.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.slope.dataFile.domain.SlopeDataFile;
import com.ruoyi.slope.dataFile.service.ISlopeDataFileService;
import com.ruoyi.slope.gateway.service.ISlopeGatewaySoundAlarm;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.slope.gateway.domain.SlopeGatewayInfo;
import com.ruoyi.slope.gateway.service.ISlopeGatewayInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 边坡网关信息Controller
 * 
 * @author psy
 * @date 2025-05-18
 */
@RestController
@RequestMapping("/slope/gateway")
public class SlopeGatewayInfoController extends BaseController
{    
    @Autowired
    private ISlopeDataFileService slopeDataFileService;
    @Autowired
    private ISlopeGatewayInfoService slopeGatewayInfoService;
    @Autowired
    private ISlopeGatewaySoundAlarm slopeGatewaySoundAlarm;

    @GetMapping("/alarm")
    public AjaxResult alarm(@RequestParam String gatewaySn)
    {
        AjaxResult res;
        int result = slopeGatewayInfoService.alarm(gatewaySn);
        if(result == 1) {
            res = success("报警成功");
        } else {
            res = error("报警失败");
        }
        return res;
    }

    @PostMapping("/sound-alarm")
    public AjaxResult alarm(@RequestBody SlopeGatewayInfo slopeGatewayInfo) {
        int result = slopeGatewaySoundAlarm.alarm(slopeGatewayInfo);

        switch (result) {
            case 1:
                return AjaxResult.success("报警成功");
            case 0:
                return AjaxResult.error(500, "参数异常");
            case -1:
                return AjaxResult.error(501, "获取 access_token 失败");
            case -2:
                return AjaxResult.error(502, "请求 access_token 接口异常");
            case -3:
                return AjaxResult.error(503, "报警请求失败，接口返回非200");
            case -4:
                return AjaxResult.error(504, "报警请求异常");
            default:
                return AjaxResult.error(500, "未知错误");
        }
    }


    @PreAuthorize("@ss.hasPermi('slope:gateway:edit')")
    @GetMapping("/cancelAlarm")
    public AjaxResult cancelAlarm(@RequestParam String gatewaySn)
    {
        int result = slopeGatewayInfoService.cancelAlarm(gatewaySn);
        if(result == 1) return success("报警成功");
        else return error("报警失败");
    }

    @PreAuthorize("@ss.hasPermi('slope:gateway:list')")
    @GetMapping("/getData")
    public AjaxResult getData(@RequestParam String gatewaySn)
    {
        int result = slopeGatewayInfoService.getData(gatewaySn);
        if(result == 1) return success("报警成功");
        else return error("报警失败");
    }

    @PreAuthorize("@ss.hasPermi('slope:gateway:list')")
    @GetMapping("/getDataTime")
    public AjaxResult getDataTime(@RequestParam String gatewaySn,@RequestParam String time)
    {
        int result = slopeGatewayInfoService.getDataTime(gatewaySn,time);
        if(result == 1) return success("报警成功");
        else return error("报警失败");
    }

    /**
     * 查询边坡网关信息列表
     */
    @PreAuthorize("@ss.hasPermi('slope:gateway:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlopeGatewayInfo slopeGatewayInfo)
    {
        startPage();
        List<SlopeGatewayInfo> list = slopeGatewayInfoService.selectSlopeGatewayInfoList(slopeGatewayInfo);
        return getDataTable(list);
    }

    /**
     * 导出边坡网关信息列表
     */
    @PreAuthorize("@ss.hasPermi('slope:gateway:export')")
    @Log(title = "边坡网关信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlopeGatewayInfo slopeGatewayInfo)
    {
        List<SlopeGatewayInfo> list = slopeGatewayInfoService.selectSlopeGatewayInfoList(slopeGatewayInfo);
        ExcelUtil<SlopeGatewayInfo> util = new ExcelUtil<SlopeGatewayInfo>(SlopeGatewayInfo.class);
        String path = util.exportExcel(response, list, "边坡网关信息数据"); 
        SlopeDataFile slopeDataFile = new SlopeDataFile();
        slopeDataFile.setFileName(path);
        slopeDataFileService.insertSlopeDataFile(slopeDataFile);
    }

    /**
     * 获取边坡网关信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('slope:gateway:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(slopeGatewayInfoService.selectSlopeGatewayInfoById(id));
    }

    /**
     * 新增边坡网关信息
     */
    @PreAuthorize("@ss.hasPermi('slope:gateway:add')")
    @Log(title = "边坡网关信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlopeGatewayInfo slopeGatewayInfo)
    {
        return toAjax(slopeGatewayInfoService.insertSlopeGatewayInfo(slopeGatewayInfo));
    }

    /**
     * 修改边坡网关信息
     */
    @PreAuthorize("@ss.hasPermi('slope:gateway:edit')")
    @Log(title = "边坡网关信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlopeGatewayInfo slopeGatewayInfo)
    {
        return toAjax(slopeGatewayInfoService.updateSlopeGatewayInfo(slopeGatewayInfo));
    }

    /**
     * 删除边坡网关信息
     */
    @PreAuthorize("@ss.hasPermi('slope:gateway:remove')")
    @Log(title = "边坡网关信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(slopeGatewayInfoService.deleteSlopeGatewayInfoByIds(ids));
    }
}
